package cn.com.dragonsoftbravo.utils.azure.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ImageProcessUtils {

	public static String WATER_BIG = "big";

	public static String WATER_MIDDLE = "middle";

	public static String WATER_SMALL = "small";

	// private static BufferedImage waterMarkBufferedImageSmall;
	//
	// private static BufferedImage waterMarkBufferedImageMiddle;
	//
	// private static BufferedImage waterMarkBufferedImageBig;

	private static int DISPLAY_IMAGE_WIDTH = 357;

	private static int DISPLAY_IMAGE_HEIGHT = 201;

	// Define the compress quality radio
	// Due to save the space of the disc, it's better to lower the image quality according to the compressRadio.
	private static double compressRadio = 0.6d;

	// static {
	// waterMarkBufferedImageSmall = getBufferedImageByResource("/water_mark_small.png");
	// waterMarkBufferedImageMiddle = getBufferedImageByResource("/water_mark_middle.png");
	// waterMarkBufferedImageBig = getBufferedImageByResource("/water_mark_big.png");
	// }

	// private static BufferedImage getBufferedImageByResource(String path) {
	// try {
	// return Thumbnails.of((InputStream) ImageProcessUtils.class.getResourceAsStream(path)).scale(1f).asBufferedImage();
	// } catch (IOException e) {
	// return null;
	// }
	// }

	public void process(BufferedImage originalImage, int originalWeight, int originalHeight, ByteArrayOutputStream oos, String waterSize)
			throws Exception {

		// BufferedImage water = null;
		//
		// if (waterSize.equals(WATER_BIG)) {
		// water = waterMarkBufferedImageBig;
		// } else if (waterSize.equals(WATER_MIDDLE)) {
		// water = waterMarkBufferedImageMiddle;
		// } else {
		// water = waterMarkBufferedImageSmall;
		// }

		//.watermark(Positions.TOP_RIGHT, water, 1f)
		Thumbnails.of(originalImage).size(originalWeight, originalHeight).outputFormat("jpg")
				.outputQuality(compressRadio).toOutputStream(oos);
	}

	public void process(BufferedImage originalImage, int originalWeight, int originalHeight, int regionWidth, int regionHeight,
			ByteArrayOutputStream oos, String waterSize) throws Exception {

//		BufferedImage water = null;
//
//		if (waterSize.equals(WATER_BIG)) {
//			water = waterMarkBufferedImageBig;
//		} else if (waterSize.equals(WATER_MIDDLE)) {
//			water = waterMarkBufferedImageMiddle;
//		} else {
//			water = waterMarkBufferedImageSmall;
//		}

		Thumbnails.of(originalImage).sourceRegion(Positions.CENTER, regionWidth, regionHeight)
				.size(originalWeight, originalHeight).outputFormat("jpg").outputQuality(compressRadio).toOutputStream(oos);
		
		//.watermark(Positions.TOP_RIGHT, water, 1f)
	}

	public ByteArrayInputStream processOriginalImage(BufferedImage originalImage, int originalWeight, int originalHeight, ByteArrayOutputStream oos)
			throws Exception {

		if (originalHeight >= 2000) {
			// If the height of original image is not less than 980px, add a big water mark to the image.
			process(originalImage, originalWeight, originalHeight, oos, ImageProcessUtils.WATER_BIG);
		} else if (originalHeight >= 980) {
			// If the height of original image is less than 980px, add a middle water mark to the image.
			process(originalImage, originalWeight, originalHeight, oos, ImageProcessUtils.WATER_MIDDLE);
		} else {
			// If the height of original image is less than 201px, add a small water mark to the image.
			process(originalImage, originalWeight, originalHeight, oos, ImageProcessUtils.WATER_SMALL);
		}

		return new ByteArrayInputStream(oos.toByteArray());
	}

	public ByteArrayInputStream processDisplayImage(BufferedImage originalImage, int originalWeight, int originalHeight, ByteArrayOutputStream tos)
			throws Exception {
		int regionWidth = originalWeight;
		int regionHeight = regionWidth * 9 / 16;

		process(originalImage, DISPLAY_IMAGE_WIDTH, DISPLAY_IMAGE_HEIGHT, regionWidth, regionHeight, tos, ImageProcessUtils.WATER_SMALL);

		return new ByteArrayInputStream(tos.toByteArray());
	}

	public ByteArrayInputStream processThumbnailImage(BufferedImage originalImage, int originalWeight, int originalHeight,
			ByteArrayOutputStream tos2, InputStream ois) throws Exception {

		if (originalHeight >= 980) {
			// If the height of original image is not less than 980, compress the height of image to 980px with the original ratio.
			int regionWidth2 = originalWeight * 980 / originalHeight;
			int regionHeight2 = 980;

			process(originalImage, regionWidth2, regionHeight2, tos2, ImageProcessUtils.WATER_MIDDLE);
			return new ByteArrayInputStream(tos2.toByteArray());
		} else {
			process(originalImage, originalWeight, originalHeight, tos2, ImageProcessUtils.WATER_SMALL);
			return new ByteArrayInputStream(tos2.toByteArray());
		}

	}

}
